<!--
- Author:   .
- Date:    2017/8/2 0002.
- File:    index.
-->
<template>
  <div class="">
    <!--<span @click="" class="close"><i class="iconfont icon-guanbi"></i></span>-->
    <div class="logo2"><img src="../../assets/images/lgoo.png"></div>
    <div class="login_cont">
      <div class="login_ipt">
        <input type="tel" placeholder="手机号码" v-model="mobile">
      </div>
      <div class="login_ipt">
        <input type="password" placeholder="密码" v-model="pwd"><label>
      </label>
      </div>
      <div class="login_btn">
        <button @click="login">密码登录</button>
      </div>
      <div class="login_other" style=" padding-top:10px;margin-top: 20px;">
        <ul class="clear">
          <li style=" float:left; text-align:left;">
            <a href="javascript:void(0)" @click="gotoModifyPwd"><span>忘记密码</span></a>
          </li>
          <li style=" float:right; text-align:right;">
            <a href="javascript:void(0)" @click="gotoLoginMessage"><span>快速注册</span></a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
  import messageCode from '../../components/message-code.vue'

  export default{
    beforeRouteEnter (to, from, next) {
      if (!document.body.classList.contains('login_bg')) {
        document.body.classList.add('login_bg')
      }
      next()
    },
    beforeRouteLeave  (to, from, next) {
      if (to.name != 'login-password' && to.name != 'login-bind') {
        document.body.classList.remove('login_bg')
      }
      next()
    },
    components: {
      'message-code': messageCode
    },
    data() {
      return {
        fkmid: this.$storage.getFkmid() || null,
        mobile: '',
        pwd: ''
      }
    },
    methods: {
      gotoModifyPwd() {
        this.$router.push({name: 'setting-password'})
      },
      gotoLoginMessage() {
        this.$router.replace({name: 'login-message'})
      },
      login() {
        if (!this.$validator.phone(this.mobile)) {
          this.$toast('请输入正确的手机号')
          return
        }
        if (this.pwd == '') {
          this.$toast('请输入密码')
          return
        }
        this.$request.loginByPwd(this.mobile, this.pwd).then(data => {
          this.$storage.setKey(data.key)
//          if (!data.member_truename) {
//            this.$toast('请先填写真实姓名')
//            this.$router.replace({name: 'setting-truename', query: {fkmid: this.fkmid}})
//          } else {
          this.$router.replace({name: 'goods-detail', query: {fkmid: this.fkmid}})
//          }
        }).catch(e => {
          this.$toast('登录失败:' + e.msg)
        })
      },
      wxLogin() {
        this.$native.weixinLogin().then(wxInfo => {
          console.log('微信登录成功', wxInfo)
          this.wxInfo = wxInfo
          this.$request.getKeyByUnionId(wxInfo.unionid).then(data => {
            console.log('根据unionid获取用户信息', data)
            if (data.key) {
              this.$storage.setKey(data.key)
              this.$router.replace({name: 'home', query: {fkmid: this.fkmid}})
            } else {
              this.$router.replace({name: 'login-bind'})
            }
          })
        }).catch(e => {
          this.$toast('微信登录失败:' + e.msg)
        })
      }
    }
  }
</script>

<style>

</style>
